package com.qiqitrue.caipiao.lottery.controller;

import java.util.Iterator;

import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheKit;
import com.qiqitrue.caipiao.lottery.model.Lottery;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class LotteryController extends Controller {

	public void index() {
		// 用户ID 必填
		String uid = getPara("uid");

		String callback = getPara("callback");
		String json = "{\"status\":{\"code\":\"404\",\"text\":\"uid不能为空\"}}";
		if (StrKit.isBlank(uid)) {
			renderJson(jsonp(callback, json));
			return;
		}
		
		// 通信验证 必填
		String token = getPara("token");
		if(StrKit.isBlank(token)) {
			renderJson(jsonp(callback, "{\"status\":{\"code\":\"404\",\"text\":\"token不能为空\"}}"));
			return;
		}
		
		if(!StrKit.equals(token, PropKit.use("users.setting").get(uid))) {
			renderJson(jsonp(callback, "{\"status\":{\"code\":\"404\",\"text\":\"token校验失败,请联系QQ: 1120121072\"}}"));
			return;
		}
		// code
		Object code = getPara("code", "code");

		// 输出格式 选填
		String format = getPara("format", "json");
		Object object = CacheKit.get("lotterys", code);
		if(object == null) {
			renderJson(jsonp(callback, "{\"status\":{\"code\":\"404\",\"text\":\"接口没有租用\"}}"));
			return;
		}
		Lottery lottery = (Lottery)object;
		// 暂且只能查询一条，最适用于市场，同时的设计也开放日后显示多条记录的情况
		if (format.equalsIgnoreCase("json")) {
			renderJson(jsonp(callback, "{\"rows\":\"1\",\"code\":\""+lottery.getLotCode()+"\",\"name\":\""+lottery.getName()+"\",\"describe\":\""+lottery.getExplain()+"\",\"info\":\"专业的事找专业的人,如有需要请加QQ:1120121072(注明彩票或API)\",\"data\":[{\"expect\":\""+lottery.getPreDrawIssue()+"\",\"opencode\":\""+lottery.getPreDrawCode()+"\",\"opentime\":\""+lottery.getDrawTime()+"\",\"opentimestamp\":\""+System.currentTimeMillis()+"\"}]}"));
			return;
		} else if (format.equalsIgnoreCase("xml")) {
			setAttr("lottery", lottery);
			renderXml("lottery.xml");
			return;
		}
		renderJson(jsonp(callback, "{\"status\":{\"code\":\"404\",\"text\":\"输出格式错误，请设置format：json 或 xml\"}}"));
	}
	
	public void cj() {
		String url = getPara("url", "https://api.api68.com/lottery/getLotteryListByType.do?&type=4");
		String lotteryStr = HttpKit.get(url);
		JSONObject lotteryJsonObj = JSONUtil.parseObj(lotteryStr);
		String errorCode = lotteryJsonObj.getStr("errorCode");
		if(StrKit.equals("0", errorCode)) {
			JSONArray dataJsonArray = lotteryJsonObj.getJSONObject("result").getJSONArray("data");
			for (Iterator<Object> iterator = dataJsonArray.iterator(); iterator.hasNext();) {
				JSONObject jsonObject = (JSONObject)iterator.next();
				String lotCode = jsonObject.getStr("lotCode");
				String lotName = jsonObject.getStr("lotName");
				String preDrawIssue = jsonObject.getStr("preDrawIssue");
				String drawTime = jsonObject.getStr("drawTime");
				String preDrawCode = jsonObject.getStr("preDrawCode");
				String frequency = jsonObject.getStr("frequency");
				String totalCount = jsonObject.getStr("totalCount");
				Lottery lottery = new Lottery(null, null, lotCode, lotName, lotName, preDrawIssue, preDrawCode, frequency, drawTime);
				lottery.setTotalCount(totalCount);
//				System.out.println(lottery);
				CacheKit.put("lotterys", lotCode, lottery);
			}
			renderJson(dataJsonArray);
			return;
		}
		renderText("采集失败");
	}

	public String jsonp(String callback, String json) {
		if (StrKit.notBlank(callback)) {
			json = callback + "(" + json + ")";
		}
		return json;
	}
}
